package com.ruoyi.system.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @Description: TODO
 * @author: wangp
 * @date: 2025年03月25日 14:42
 */
public class CompressUtil {
    public static void main(String[] args) {
        String zipFilePath = "D:\\test\\348494400390709248.zip"; // ZIP 文件路径
        String destDir = "D:\\test"; // 解压目的地

        unzip(zipFilePath, destDir);
    }

    public static void unzip(String zipFilePath, String destDir) {
        File dir = new File(destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        try (ZipFile zipFile = new ZipFile(zipFilePath)) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File file = new File(dir, entry.getName());

                // 创建目录
                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    // 确保父目录存在
                    new File(file.getParent()).mkdirs();
                    // 解压文件
                    try (InputStream is = zipFile.getInputStream(entry)) {
                        Files.copy(is, file.toPath());
                    }
                }
                System.out.println("Extracted: " + file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
